package testes.vSys.action;

import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import testes.vSys.bean.Brand;
import testes.vSys.bean.Color;
import testes.vSys.bean.Fuel;
import testes.vSys.bean.Model;
import testes.vSys.bean.Vehicle;
import testes.vSys.dao.DAOFactory;
import testes.vSys.dao.IModelDao;
import testes.vSys.dao.IVehicleDao;
import testes.vSys.form.VehicleConsultationForm;
import testes.vSys.util.Utilities;

public class VehicleConsultationAction extends DispatchAction{

	private final String VEHICLES_COLLECTION = "vehiclesCollection";
	private final String FUELS_COLLECTION = "fuelsCollection";
	private final String COLORS_COLLECTION = "colorsCollection";
	private final String BRANDS_COLLECTION = "brandsCollection";
	private final String MODELS_COLLECTION = "modelsCollection";
		
	public ActionForward load(ActionMapping mapping, ActionForm form, HttpServletRequest request, 
												HttpServletResponse response) throws Exception {

				
		IVehicleDao vehicleDAO = DAOFactory.getVehicleDao();
		IModelDao modelDAO = DAOFactory.getModelDao();
		
		Collection<Color> colors = vehicleDAO.getAvaiableColors();
		Collection<Fuel> fuels = vehicleDAO.getAvaiableFuels();
		Collection<Brand> brands = modelDAO.getAvaiableBrands();
		
				
		request.setAttribute(FUELS_COLLECTION, fuels);
		request.setAttribute(COLORS_COLLECTION, colors);
		request.setAttribute(BRANDS_COLLECTION, brands);

		return mapping.findForward("vehicleConsultation");
	}
	public ActionForward changeModel(ActionMapping mapping, ActionForm form, HttpServletRequest request, 
			HttpServletResponse response) throws Exception {
		
		VehicleConsultationForm vehicleForm = (VehicleConsultationForm)form;
		
		String brandId = vehicleForm.getSelectedBrand();
		
		if (!brandId.equals("")){
			
			IModelDao modelDAO = DAOFactory.getModelDao();
			
			Brand brand = modelDAO.getBrandById(Integer.valueOf(brandId).intValue());
			
			Collection<Model> models = modelDAO.getModelsByBrand(brand);
			
			request.setAttribute(MODELS_COLLECTION, models);
		}		
		
		
		return load(mapping, form, request, response);
	}
	
	public ActionForward consulta(ActionMapping mapping, ActionForm form, HttpServletRequest request, 
			HttpServletResponse response) throws Exception {
		
		VehicleConsultationForm vehicleForm = (VehicleConsultationForm)form;
		IVehicleDao vehicleDAO = DAOFactory.getVehicleDao();
		
		String vehicleId = vehicleForm.getVehicleId();
		String plate = vehicleForm.getPlate();
		String initYear = vehicleForm.getInitYear();
		String endYear = vehicleForm.getEndYear();
		String initValue = vehicleForm.getInitValue();
		String endValue = vehicleForm.getEndValue();
		String color = vehicleForm.getSelectedColor(); 
		String model = vehicleForm.getSelectedModel();
		String brand = vehicleForm.getSelectedBrand();
		String fuel = vehicleForm.getSelectedFuel();
		String city = vehicleForm.getCity();
		String state = vehicleForm.getState();
		
		Collection<Vehicle> vehicles = vehicleDAO.getVehicle(vehicleId, plate, initYear, endYear, initValue, 
															endValue, color, model, brand, fuel, city, state);
		
		request.setAttribute(VEHICLES_COLLECTION, vehicles);
			
		
		return load(mapping, form, request, response);		
	}
}
